package cn.koala.key.complaint.service.impl;

import cn.koala.key.api.ComplaintApi;
import cn.koala.key.common.feign.FeignUtils;
import cn.koala.key.common.utils.DateUtils;
import cn.koala.key.common.utils.BaseEnumUtil;
import cn.koala.key.complaint.entity.dto.ComplaintSearchDTO;
import cn.koala.key.complaint.entity.dto.ComplaintSearchModel;
import cn.koala.key.complaint.entity.enums.ComplaintOrderStatesEnum;
import cn.koala.key.complaint.entity.enums.ComplaintSortEnum;
import cn.koala.key.complaint.entity.enums.ComplaintSourceEnum;
import cn.koala.key.complaint.entity.enums.ComplaintValidEnum;
import cn.koala.key.complaint.entity.vo.*;
import cn.koala.key.complaint.service.ComplaintOrderService;
import cn.seed.common.core.Result;
import cn.seed.common.core.ResultList;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 客诉单服务
 *
 * @author 38752
 */
@Service
public class ComplaintOrderServiceImpl implements ComplaintOrderService {
    @Override
    public Result<?> addComplaintOrder(CustomerComplaintOrderVO complaintOrderVO, String oprNo, String oprName) {
        return FeignUtils.getHttpApi(ComplaintApi.class).addComplaintOrder(complaintOrderVO, oprNo, oprName);
    }

    @Override
    public Result<?> processComplaintOrder(String orderNo, String oprNo, String oprName) {
        return FeignUtils.getHttpApi(ComplaintApi.class).processComplaintOrder(orderNo, oprNo, oprName);
    }

    @Override
    public Result<?> commitCompliantOrder(CustomerComplaintOrderSubmitVO complaintOrderVO, String oprNo, String oprName) {
        return FeignUtils.getHttpApi(ComplaintApi.class).commitCompliantOrder(complaintOrderVO, oprNo, oprName);
    }

    @Override
    public Result<?> cityApproveComplaintOrder(CustomerComplaintOrderApproveVO customerComplaintOrderApproveVO, String oprNo, String oprName) {
        return FeignUtils.getHttpApi(ComplaintApi.class).cityApproveComplaintOrder(customerComplaintOrderApproveVO, oprNo, oprName);
    }

    @Override
    public Result<?> hqApproveComplaintOrder(CustomerComplaintOrderApproveVO customerComplaintOrderApproveVO, String oprNo, String oprName) {
        return FeignUtils.getHttpApi(ComplaintApi.class).hqApproveComplaintOrder(customerComplaintOrderApproveVO, oprNo, oprName);
    }

    @Override
    public Result<ComplaintOperateLogVO> getRefuseReason(String orderNo) {
        return FeignUtils.getHttpApi(ComplaintApi.class).getRefuseReason(orderNo);
    }

    @Override
    public Result<List<ComplaintOperateLogVO>> listOperateLogByOrderNo(String orderNo) {
        return FeignUtils.getHttpApi(ComplaintApi.class).listOperateLogByOrderNo(orderNo);
    }

    @Override
    public Result<ComplaintOperateLogVO> getOperateLogById(Long id) {
        return FeignUtils.getHttpApi(ComplaintApi.class).getOperateLogById(id);
    }

    @Override
    public Result<ComplaintOrderDetailVO> orderDetail(String orderNo) {
        return FeignUtils.getHttpApi(ComplaintApi.class).orderDetail(orderNo);
    }

    @Override
    public Result<ResultList<ComplaintSearchDTO>> searchByCondition(ComplaintSearchVO complaintSearchVO) {
        return FeignUtils.getHttpApi(ComplaintApi.class).searchByCondition(complaintSearchVO);
    }

    @Override
    public Result<ComplaintOrderFillResultVO> fillComplaintOrder(String orderNo, String repairOrderNo) {
        return FeignUtils.getHttpApi(ComplaintApi.class).fillComplaintOrder(orderNo, repairOrderNo);
    }

    @Override
    public List<ComplaintSearchModel> complaintSearchPrint(ComplaintSearchVO complaintSearchVO) {
        Result<ResultList<ComplaintSearchDTO>> complaints = FeignUtils.getHttpApi(ComplaintApi.class).searchByCondition(complaintSearchVO);
        return convertComplaintSearchModel(complaints.getResult().getData());
    }

    private List<ComplaintSearchModel> convertComplaintSearchModel(List<ComplaintSearchDTO> complaintSearchList) {
        List<ComplaintSearchModel> complaintSearchModels = new ArrayList<>();
        complaintSearchList.forEach(data -> {
            ComplaintSearchModel complaintSearchModel = new ComplaintSearchModel();
            complaintSearchModel.setCityName(data.getCityName());
            complaintSearchModel.setStoreName(data.getStoreName());
            complaintSearchModel.setCarNo(data.getCarNo());
            complaintSearchModel.setCreateTimeStart(DateUtils.format(data.getComplaintDate()));
            complaintSearchModel.setCreateTimeEnd(DateUtils.format(data.getCloseDate()));
            complaintSearchModel.setOrderNo(data.getOrderNo());
            ComplaintSortEnum complaintReasonEnum = BaseEnumUtil.getByCode(data.getSort(), ComplaintSortEnum.class);
            if (complaintReasonEnum != null) {
                complaintSearchModel.setSortName(complaintReasonEnum.getCaption());
            }
            ComplaintSourceEnum complaintWayEnum = BaseEnumUtil.getByCode(data.getSource(), ComplaintSourceEnum.class);
            if (complaintWayEnum != null) {
                complaintSearchModel.setSourceName(complaintWayEnum.getCaption());
            }
            ComplaintOrderStatesEnum complaintOrderStatesEnum = BaseEnumUtil.getByCode(data.getStatus(), ComplaintOrderStatesEnum.class);
            if (complaintOrderStatesEnum != null) {
                complaintSearchModel.setStatus(complaintOrderStatesEnum.getCaption());
            }
            ComplaintValidEnum complaintValidEnum = BaseEnumUtil.getByCode(data.getIsValid(), ComplaintValidEnum.class);
            if (complaintValidEnum != null) {
                complaintSearchModel.setValid(complaintValidEnum.getCaption());
            }
            complaintSearchModels.add(complaintSearchModel);
        });
        return complaintSearchModels;
    }
}
